const BaseModel = require('./BaseModel');
class AdminModel extends BaseModel {
  constructor() {
    super();
    this.tableName = 'im_admin_user';
    this.getTableFields(this.tableName).then(fields => {
      this.fields = fields;
    });
  }

  async login(phone, password) {
    try {
      const fields = this.fields.filter(item => {
        return item !== 'password';
      }).join(',');

      const sql = `select ${fields} from ${this.tableName} where phone = ? and password = ?`;
      const params = [phone, password];
      const results = await this.query(sql, params);
      const data = results[0];



      if (results.length > 0) {
        return {
          code: 200,
          msg: '登录成功',
          data: {
            id: data.id,
            phone: data.phone,
            username: data.username,
            created_t: data.created_t
          }
        }
      }
      return {
        code: 400,
        msg: '账号或密码错误'
      }
    } catch (e) {
      console.log(e);
    }
  }
}


module.exports = new AdminModel();